#pragma once
//根据设计好的题库建立model，用来与用户进行数据交互
#include<iostream>
#include<vector>
#include<unordered_map>
#include<fstream>
#include<cassert>
#include"include/mysql.h"

#include"../Com/Log.hpp"
#include"../Com/Util.hpp"

namespace ns_model
{
    using namespace ns_LOG;
    using namespace ns_Util;
    const std::string oj_questions = "oj_questions";
    const std::string host = "192.168.52.133";
    const std::string user = "oj_client";
    const std::string password = "3187907625lhx.";
    const std::string database = "oj";
    int port = 3306;

    struct Question
    {
        std::string number; //题目编号，唯一
        std::string title;  //题目的标题
        std::string star;   //难度: 简单 中等 困难
        std::string desc;   //题目的描述
        std::string header; //题目预设给用户在线编辑器的代码
        std::string tail;   //题目的测试用例，需要和header拼接，形成完整代码
        int cpu_limit;      //题目的时间要求(S)
        int mem_limit;      //题目的空间要求(KB)
    };

    class Model
    {
    private:
        std::unordered_map<std::string, Question> questions; //题目编号和题目映射
    public:
        Model()
        {}

        ~Model(){}
    public:
        //执行sql语句
        bool QueryMysql(const std::string &sql, std::vector<Question> *out)
        {
            //获取mysql句柄
            MYSQL *mysql = mysql_init(nullptr);

            //连接数据库
            if(mysql_real_connect(mysql,host.c_str(),user.c_str(),password.c_str(),database.c_str(),port,nullptr,0) == nullptr)
            {
                LOG(FATAL) << "Connect to mysql failed!" << "\n";
                std::cout << mysql_error(mysql) << std::endl;
                return false;
            }

            //设置编码
            mysql_set_character_set(mysql,"utf8");
            LOG(INFO) << "Connect to mysql success!" << "\n";

            //执行sql语句
            if(mysql_query(mysql,sql.c_str()) != 0)
            {
                LOG(FATAL) << "Execute sql failed!" << "\n";
                return false;
            }

            //获取结果集
            MYSQL_RES *res = mysql_store_result(mysql);

            //获取结果集的行列数
            int num_rows = mysql_num_rows(res);
            int num_fields = mysql_num_fields(res);

            Question q;
            for(int i = 0; i < num_rows; i++)
            {
                //获取行内容并填充到Question结构体中
                MYSQL_ROW row = mysql_fetch_row(res);
                q.number = row[0];
                q.title = row[1];
                q.star = row[2];
                q.desc = row[3];                
                q.header = row[4];
                q.tail = row[5];
                q.cpu_limit = atoi(row[6]);
                q.mem_limit = atoi(row[7]);
                out->push_back(q);
            }

            //释放资源
            free(res);
            mysql_close(mysql);
            return true;
        }

        //获取所有题目
        bool GetAllQuestions(std::vector<Question> *out)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            return QueryMysql(sql,out);
        }
        
        //获取指定题目
        bool GetQuestion(const std::string &number, Question *q)
        {
            std::string sql = "select * from ";
            sql += oj_questions;
            sql += " where number = ";
            sql += number;
            std::vector<Question> out;
            if(QueryMysql(sql,&out))
            {
                if(out.size() == 1)
                {
                    *q = out[0];
                    return true;
                }
            }
            return false;
        }
    };
}